<?php
namespace BaseBundle\Model;

use RedUnicorn\SymfonyKernel\Model\Model;
use Symfony\Component\DependencyInjection\ContainerInterface;
use RedUnicorn\Validate\Validate;
use BaseBundle\Entity\ArticleEvaluate;

class m_article_evaluate extends Model 
{

    /**
     * @var ArticleEvaluate
     */
    private $article_evaluate_class = null;

    function __construct(ContainerInterface $container)
    {   

        parent::__construct($container, 'article_evaluate');
    }
 
    public function validate($data, $id = false)
    {
        $this->entityManager = $this->getDoctrine()->getManager();
        
        if(empty($id)){
            $this->article_evaluate_class = new ArticleEvaluate();
        }else{
            if(!$this->is_exist($id)){
                self::$error->setError($this->get('translator')->trans('未找到对应信息'));
                return false;
            }

            $this->article_evaluate_class = $this->entityManager->getRepository(ArticleEvaluate::class)->find($id);
        }
                
        if(array_key_exists('content', $data)){
            $this->article_evaluate_class->setContent($data['content']);
        }
                
        if(array_key_exists('show_image', $data)){
            $this->article_evaluate_class->setImages($data['show_image']);
        }
                
        if(array_key_exists('like_num', $data)){
            $this->article_evaluate_class->setLikeNum($data['like_num']);
        }
                
        if(array_key_exists('created_at', $data)){
            $this->article_evaluate_class->setCreatedAt($data['created_at']);
        }
        
        if(self::$error->validate($this->article_evaluate_class)){
            return false;
        }
        
        return true;
    }
    
    public function insert($data)
    {
        if(!$this->validate($data)){
            return false;
        }
        
        
        $this->entityManager->persist($this->article_evaluate_class);
        $this->entityManager->flush();
    
        $this->lastInsertId = $this->article_evaluate_class->getId();

        return true;
    }
    
    public function update($id ,$data)
    {
        if(empty($id)){
            self::$error->setError('param id is not null');
            return false;
        }
        
        if(!$this->validate($data, $id)){
            return false;
        }
        
        $this->entityManager->flush();

        return true;
    }
    
    public function rewritesRule(&$rule)
    {   
                    
        //TODO where   
        if(array_key_exists('id', $rule) && !empty($rule['id'])){
           $this->sql_array['where'] .= " AND sql_pre.id = '{$rule['id']}' ";
        }
                    
        //TODO where   
        if(array_key_exists('content', $rule) && !Validate::isRealEmpty($rule['content'])){
            $this->sql_array['where'] .= " AND sql_pre.content LIKE '{$rule['content']}' ";
        }
                    
        //TODO where   
        if(array_key_exists('show_image', $rule) && !Validate::isRealEmpty($rule['show_image'])){
            $this->sql_array['where'] .= " AND sql_pre.show_image LIKE '{$rule['show_image']}' ";
        }
                    
        //TODO where   
        if(array_key_exists('like_num', $rule) && !empty($rule['like_num'])){
           $this->sql_array['where'] .= " AND sql_pre.like_num = '{$rule['like_num']}' ";
        }
                    
        //TODO where   
        if(array_key_exists('created_at', $rule) && !Validate::isRealEmpty($rule['created_at'])){
            $this->sql_array['where'] .= " AND sql_pre.created_at LIKE '{$rule['created_at']}' ";
        }
            
    }
       

    
}